package com.scope.customViews;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.scope.R;

/**
 * Created by Samrith on 10/9/13.
 */
public class ImageButton extends RelativeLayout implements CustomView.IAccent, Checkable {
    public ImageButton(Context context) {
        this(context, null);
    }

    public ImageButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        CustomView.FreqLog.InitializingViews(this);
        initializeViews(context);
        initializeAttrs(context, attrs);
        CustomView.FreqLog.InitializedViews(this);
    }

    protected void initializeViews(Context context) {
        mImageView = new ImageView(context);
        mImageView.setId(1);
        setImageDrawable(getResources().getDrawable(R.drawable.icon_image));
        addView(mImageView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        mCheckMark = new ImageView(context);
        mCheckMark.setImageDrawable(getResources().getDrawable(R.drawable.icon_check));
        mCheckMark.setVisibility(GONE);
        LayoutParams checkMarkLP = new LayoutParams(
                getResources().getDimensionPixelSize(R.dimen.check_width_m),
                getResources().getDimensionPixelSize(R.dimen.check_height_m));
        checkMarkLP.addRule(ALIGN_PARENT_TOP);
        checkMarkLP.addRule(ALIGN_PARENT_RIGHT);
        addView(mCheckMark, checkMarkLP);
    }

    protected void initializeAttrs(Context context, AttributeSet attrs) {
        TypedArray arr = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.ImageButton,
                0, 0
        );

        try {
            setImageDrawable(getResources().getDrawable(arr.getResourceId(R.styleable.ImageButton_image, R.drawable.icon_empty)));
            setAccent(arr.getColor(R.styleable.ImageButton_accent, android.R.color.transparent));
            setChecked(arr.getBoolean(R.styleable.ImageButton_marked, false));
        } finally {
            arr.recycle();
        }
    }

    protected int mImageWidth = 0;
    protected int mImageHeight = 0;
    protected int mImageMarginLeft = 0;
    protected int mImageMarginTop = 0;
    protected int[] mImageLayoutParamRules = null;

    protected void initImageProp(
            int width, int height,
            int left, int top,
            int... layoutParamRules) {
        mImageWidth = width;
        mImageHeight = height;
        mImageMarginLeft = left;
        mImageMarginTop = top;
        mImageLayoutParamRules = layoutParamRules;
    }

    protected void reviseImageSize(int width, int height) {
        int left = mImageMarginLeft;
        int top = mImageMarginTop;

        if (width == LayoutParams.MATCH_PARENT) {
            left = 0;
        }

        if (height == LayoutParams.MATCH_PARENT) {
            top = 0;
        }

        LayoutParams lp = new LayoutParams(width, height);
        if (mImageLayoutParamRules != null && mImageLayoutParamRules.length != 0) {
            for (int rule : mImageLayoutParamRules) {
                lp.addRule(rule);
            }
        }

        lp.setMargins(left, top, 0, 0);
        mImageView.setLayoutParams(lp);
    }

    public void setImageDrawable(Drawable image) {
        if (mImageView != null) {
            if (image != null) {
                mImageView.setImageDrawable(image);
            } else {
                mImageView.setImageDrawable(getResources().getDrawable(R.drawable.icon_empty));
            }

            mImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        }
    }

    public void setImageBitmap(Bitmap bmp) {
        setImageDrawable(new BitmapDrawable(getResources(), bmp));
    }

    public Drawable getDrawable() {
        return mImageView.getDrawable();
    }

    protected int mAccent = R.color.Transparent;

    public int getAccent() {
        return mAccent;
    }

    public void setAccent(int color) {
        mAccent = color;
        if (mImageView != null) {
            mImageView.setBackgroundColor(color);
        }
    }

    protected boolean mMarkVisible = false;

    public void setChecked(boolean visible) {
        if (visible != mMarkVisible) {
            mMarkVisible = visible;

            if (visible) {
                mCheckMark.setVisibility(View.VISIBLE);
            } else {
                mCheckMark.setVisibility(View.GONE);
            }
        }
    }

    public boolean isChecked() {
        return mMarkVisible;
    }

    public void toggle() {
        setChecked(!mMarkVisible);
    }

    protected ImageView mImageView = null;
    protected ImageView mCheckMark = null;
}
